# coding=utf-8
def main():
	# 'while you look at the csv file '
	# csv意指comma separated value file format
	# 'http://www.pythonchallenge.com/pc/ring/yankeedoodle.csv'
	# Load the CSV data
	f = open(ur'yankeedoodle.csv')
	data = ' '.join(f.read().splitlines())
	f.close()
	fields = data.split(', ')

	# Making sense of the numbers
	print len(fields)
	# 7367

	n = len(fields)
	def factor(n):
		d = 2
		factors = []
		while not n % d:
			factors.append(d)
			n /= d
		d = 3
		while n > 1 and d * d <= n:
			if not n % d:
				factors.append(d)
				n /= d
			else:
				d += 2
		if n > 1:
			factors.append(n)
		return factors
	print factor(n)
	# [53, 139]

	# Imaging the numbers
	import Image
	im = Image.new('F', (53, 139))
	im.putdata(map(float, fields), 256)
	im = im.transpose(Image.FLIP_LEFT_RIGHT)	
	im = im.transpose(Image.ROTATE_90)
	im.show()
	# n = str(x[i])[5] + str(x[i+1])[5] + str(x[i+2])[6]

	nlist = []
	for i in range(0, n-2, 3):
		n = chr(int(fields[i][5] + fields[i+1][5] + fields[i+2][6]))
		nlist.append(n)
	print ''.join(nlist)
	# So, you found the hidden message.
	# There is lots of room here for a long message, but we only need very little space to say "look at grandpa", so the rest is just garbage. 
	# 下一关：http://www.pythonchallenge.com/pc/ring/grandpa.html

if __name__ == '__main__':
	main()
